package com.xbongbong.pay.platform.wechatpay.config;

import com.xbongbong.pay.platform.wechatpay.utils.WeChatPayCertUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 *
 * @author 魏荣杰
 * @date 2021/1/22 19:57
 * @since
 * @version
 */
@Configuration
@Conditional(WeChatPayConfigCondition.class)
@EnableConfigurationProperties(WeChatPayConfig.class)
public class WeChatPayMetaConfiguration {
    /**
     * The constant CERT_ALIAS.
     */
    private static final String CERT_ALIAS = "Tenpay Certificate";

    private static final Logger LOG = LoggerFactory.getLogger(WeChatPayMetaConfiguration.class);

    @Bean
    @ConditionalOnMissingBean
    public WeChatPayMetaContainer weChatPayMetaContainer(WeChatPayConfig weChatPayConfig) {
        WeChatPayMetaContainer weChatPayMetaContainer = new WeChatPayMetaContainer();
        Map<String, WeChatPayConfig.V3> v3 = weChatPayConfig.getV3();
        v3.forEach((k, v) -> {
            WeChatPayMeta weChatPayMeta = WeChatPayCertUtil.createMeta(v.getCertPath(), CERT_ALIAS, v.getMchId(), "PKCS12");
            weChatPayMeta.setV3(v);
            weChatPayMeta.setMchId(k);
            weChatPayMetaContainer.addWechatMeta(k, weChatPayMeta);
        });
        return weChatPayMetaContainer;
    }

}
